﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Corvus.Widgets.Models;
using Newtonsoft.Json;
using System.IO;
using MbUnit.Framework;
using System.Globalization;

namespace Corvus
{
    public static class WhenBuildingPages
    {

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
        public static void WidgetsCanBeSerializedAndDeserialized()
        {
            IPage p = new Page();
            p.QuoteText = "Test Content Snippet";
            p.Name = "Test Page";
            p.Slug = "test-page";
            p.PageTitle = "Test Content";
            var list = new WidgetList();
            list.Widgets.Add(new BasicContent { Content = "<h1>Test</h1><p>Test Content</p>" });
            p.WidgetZones[CommonZones.MainArea] = new WidgetList();
            var js = new JsonSerializer();
            js.TypeNameHandling = TypeNameHandling.All;
            js.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
            StringBuilder sb = new StringBuilder();
            using (var sw = new StringWriter(sb, CultureInfo.InvariantCulture))
            {
                js.Serialize(sw, p);
            }
            using (var sr = new StringReader(sb.ToString()))
            {
                using (var jtr = new JsonTextReader(sr))
                {
                    var deserialized = js.Deserialize<IPage>(jtr);
                    Assert.AreEqual(p.Name, deserialized.Name);
                    Assert.AreSame(p.WidgetZones[CommonZones.MainArea].GetType(), deserialized.WidgetZones[CommonZones.MainArea].GetType());
                }
            }

        }

    }
}
